package com.vehicle.service.Impl;

import com.vehicle.domains.entity.SysPermissionEntity;
import com.vehicle.domains.bo.QueryPermissionBo;
import com.vehicle.exception.VehicleAccessException;
import com.vehicle.mapper.SysPermissionMapper;
import com.vehicle.service.SysPermissionService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Resource
    private SysPermissionMapper sysPermissionMapper;

    @Override
    public void addPermission(SysPermissionEntity sysPermissionEntity) {
        try {
            sysPermissionMapper.insertPermission(sysPermissionEntity);
        } catch (DuplicateKeyException e) {
            throw VehicleAccessException.DATA_EXIST.newInstance("权限已存在");
        }
    }

    @Override
    public void updatePermission(SysPermissionEntity sysPermissionEntity) {
        int i = sysPermissionMapper.updatePermissionById(sysPermissionEntity);
        if (i == 0) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("修改权限未修改到数据库");
        }
    }

    @Override
    public void deletePermission(Long id) {
        int i = sysPermissionMapper.deletePermissionById(id);
        if (i == 0) {
            throw VehicleAccessException.DATA_NOT_EXIST.newInstance("删除权限未删除到数据库");
        }
    }

    @Override
    public List<SysPermissionEntity> selectPermissionList(QueryPermissionBo permissionBo) {
        return sysPermissionMapper.selectPermissionList(permissionBo);
    }

    @Override
    public SysPermissionEntity selectByPermissionId(Long id) {
        return sysPermissionMapper.selectPermissionById(id);
    }
}
